package com.im.service.docReview.smo;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.im.service.dto.PageQuery;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.im.service.dto.TenderInfo;
import com.im.service.tenderInfo.dao.ITenderInfoQueryDAO;
import com.im.util.ListUtil;
import com.im.util.ReturnsUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.im.service.docReview.dao.IDocReviewQueryDAO;
import com.im.service.dto.DocManage;
import com.im.service.dto.DocReviewPlan;
import com.im.service.dto.DocReviewRecord;
import com.im.service.dto.DocReviewResult;
import com.im.service.dto.DocReviewer;
import com.im.service.dto.FileUpload;
import com.im.service.dto.PurchasePlan;
import com.im.util.JsonUtil;
import com.im.viewmodel.docReview.DocReviewReqVo;

@Service
public class IDocReviewQuerySMOImpl implements IDocReviewQuerySMO {
	@Autowired
	private IDocReviewQueryDAO docReviewQueryDAO;
	@Autowired
	private ITenderInfoQueryDAO tenderInfoQueryDAO;

	@Override
	public DocReviewResult queryDocReviewResultByDocManageId(Map<String, Object> param) {
		return docReviewQueryDAO.queryDocReviewResultByDocManageId(param);
	}

	@Override
	public PageQuery<DocReviewReqVo> queryDocReviewList(String inJson) {
		String impleProjId = JsonUtil.obtainValueFromJsonObject(inJson, "impleProjId");
		String planProjClass = JsonUtil.obtainValueFromJsonObject(inJson, "planProjClass");
		String purchasePlanId = JsonUtil.obtainValueFromJsonObject(inJson, "purchPlanId");
		String orgId = JsonUtil.obtainValueFromJsonObject(inJson, "orgId");
        String purchaseStatusCd = JsonUtil.obtainValueFromJsonObject(inJson, "purchaseStatusCd");
		Map<String, Object> purchaseMap = new HashMap<String, Object>();
		purchaseMap.put("impleProjId", impleProjId);
		purchaseMap.put("planProjClass", planProjClass);
		purchaseMap.put("purchPlanId", purchasePlanId);
		purchaseMap.put("orgId", orgId);
        purchaseMap.put("statusCd", purchaseStatusCd);
		PageQuery<DocReviewReqVo> page = JsonUtil.jsonToBean(inJson, PageQuery.class);
		List<DocReviewReqVo> respVoList = new ArrayList<DocReviewReqVo>();
		PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        List<TenderInfo> tenderInfos = tenderInfoQueryDAO.queryTenderInfoListForDocReview(purchaseMap);
		if (tenderInfos.size() > 0) {
			for (TenderInfo tenderInfo : tenderInfos) {
                DocReviewReqVo docReviewReqVo = new DocReviewReqVo();
                docReviewReqVo.setTenderInfo(tenderInfo);
                String tenderInfoId = tenderInfo.getTenderInfoId();
				Map<String, Object> docManageMap = new HashMap<String, Object>();
				docManageMap.put("relaObjId", tenderInfoId);
                docManageMap.put("relaObjType","30");
				List<DocManage> docManageList = docReviewQueryDAO.queryDocManageListByRelaObjId(docManageMap);
				if(!ListUtil.isListEmpty(docManageList)){
				    for(DocManage docManage :docManageList){
                        docReviewReqVo.setDocManage(docManage);
                        String docManageId = docManage.getDocManageId();
                        // 内部评审计划
                        Map<String, Object> internalReviewMap = new HashMap<String, Object>();
                        internalReviewMap.put("docManageId", docManageId);
                        internalReviewMap.put("revierType", 10);
                        DocReviewPlan internalReviewPlan = docReviewQueryDAO.queryDocReviewPlanByDocManageId(internalReviewMap);
                        docReviewReqVo.setInternalReviewPlan(internalReviewPlan);
                        if (internalReviewPlan != null) {
                            // 内部评审记录
                            String internalReviewPlanId = internalReviewPlan.getReviewPlanId();
                            Map<String, Object> internalReviewRecordMap = new HashMap<String, Object>();
                            internalReviewRecordMap.put("reviewPlanId", internalReviewPlanId);
                            DocReviewRecord internalReviewRecord = docReviewQueryDAO.queryDocReviewRecordByReviewPlanId(internalReviewRecordMap);
                            docReviewReqVo.setInternalReviewRecord(internalReviewRecord);
                            List<DocReviewer> internalReviewerList = docReviewQueryDAO.queryDocReviewerByReviewPlanId(internalReviewRecordMap);
                            StringBuilder internalReviewSb = new StringBuilder();
                            if (internalReviewerList.size() > 0) {
                                docReviewReqVo.setInternalReviewerList(internalReviewerList);
                                for (int i = 0; i < internalReviewerList.size(); i++) {
                                    DocReviewer docReviewer = internalReviewerList.get(i);
                                    String docReviewerName = docReviewer.getReviewer();
                                    if (i < internalReviewerList.size() - 1) {
                                        internalReviewSb.append(docReviewerName + ",");
                                    } else {
                                        internalReviewSb.append(docReviewerName);
                                    }
                                }
                                String internalReviewers = internalReviewSb.toString();
                                docReviewReqVo.setInternalReviewers(internalReviewers);
                            }
                        }
                        // 专家评审计划
                        Map<String, Object> expertReviewMap = new HashMap<String, Object>();
                        expertReviewMap.put("docManageId", docManageId);
                        expertReviewMap.put("revierType", 20);
                        DocReviewPlan expertReviewPlan = docReviewQueryDAO.queryDocReviewPlanByDocManageId(expertReviewMap);
                        docReviewReqVo.setExpertReviewPlan(expertReviewPlan);
                        if (expertReviewPlan != null) {
                            // 专家评审记录
                            String expertReviewPlanId = expertReviewPlan.getReviewPlanId();
                            Map<String, Object> expertReviewRecordMap = new HashMap<String, Object>();
                            expertReviewRecordMap.put("reviewPlanId", expertReviewPlanId);
                            DocReviewRecord expertReviewRecord = docReviewQueryDAO.queryDocReviewRecordByReviewPlanId(expertReviewRecordMap);
                            docReviewReqVo.setExpertReviewRecord(expertReviewRecord);
                            List<DocReviewer> expertReviewerList = docReviewQueryDAO.queryDocReviewerByReviewPlanId(expertReviewRecordMap);
                            StringBuilder expertReviewSb = new StringBuilder();
                            if (expertReviewerList.size() > 0) {
                                docReviewReqVo.setExpertReviewerList(expertReviewerList);
                                for (int i = 0; i < expertReviewerList.size(); i++) {
                                    DocReviewer docReviewer = expertReviewerList.get(i);
                                    String docReviewerName = docReviewer.getReviewer();
                                    if (i < expertReviewerList.size() - 1) {
                                        expertReviewSb.append(docReviewerName + ",");
                                    } else {
                                        expertReviewSb.append(docReviewerName);
                                    }
                                }
                                String expertReviewers = expertReviewSb.toString();
                                docReviewReqVo.setExpertReviewers(expertReviewers);
                            }
                        }
                        // 评审结果
                        Map<String, Object> docReviewResultMap = new HashMap<String, Object>();
                        docReviewResultMap.put("docManageId", docManageId);
                        DocReviewResult docReviewResult = docReviewQueryDAO.queryDocReviewResultByDocManageId(docReviewResultMap);
                        docReviewReqVo.setDocReviewResult(docReviewResult);

                        // 附件查询
                        Map<String, Object> fileUploadMap = new HashMap<String, Object>();
                        fileUploadMap.put("uploadObjId", docManageId);
                        fileUploadMap.put("uploadObjType", 60);
                        List<FileUpload> fileUpload = docReviewQueryDAO.queryFileUploadInfosForDocManage(fileUploadMap);
                        if (fileUpload.size() > 0) {
                            docReviewReqVo.setFileUpload(fileUpload);
                        }
                    }
                }
                //采购计划信息
                if(null!=tenderInfo.getPurchPlanId()){
                    Map<String, Object> tenderInfdMap = new HashMap<String, Object>();
                    tenderInfdMap.put("purchPlanId", tenderInfo.getPurchPlanId());
                    List<PurchasePlan> purchasePlanList = docReviewQueryDAO.queryPurchasePlanListForDocReview(tenderInfdMap);
                    if(!ListUtil.isListEmpty(purchasePlanList)){
                        PurchasePlan purchasePlan = purchasePlanList.get(0);
                        docReviewReqVo.setPurchasePlan(purchasePlan);
                    }
                }
                respVoList.add(docReviewReqVo);
			}
			PageInfo pageInfo = new PageInfo(respVoList);
			page.setRows(respVoList);
			page.setTotalPages(pageInfo.getPages());
			page.setTotal(pageInfo.getTotal());
		}
		return page;
	}

    @Override
    public String queryHadDocCode(String inJson) {
        String docCode = JsonUtil.obtainValueFromJsonObject(inJson, "docCode");
        Integer cnt = docReviewQueryDAO.queryHadDocCode(docCode);
        JSONObject result = new JSONObject();
        if(cnt > 0) {
            result.put("had", "true");
        } else {
            result.put("had", "false");
        }
        return ReturnsUtil.returnSuccess(result).toString();
    }
}
